package org.foliage.rcp.ui.custom;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FoliageDialog extends Dialog {

	// ----------------------------------------------- 

	private int returnCode;

	private Shell shell;

	private Control foot;

	@SuppressWarnings("unused")
	private Control body;

	private Control buttonBar;

	// ----------------------------------------------- 

	public FoliageDialog(Shell parent, int style) {
		super(parent, style);
		setText("Foliage Dialog");
	}

	// ----------------------------------------------- 

	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		shell.setSize(getInitialSize());
		foot = createFootContent(shell);
		buttonBar = createButtonBar((Composite) foot);
		body = createBodyContent(shell);
		createButtons((Composite) buttonBar);
	}

	private Control createFootContent(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		FormData data = new FormData();
		{
			data.height = 60;
			data.left = new FormAttachment(0);
			data.right = new FormAttachment(100);
			data.bottom = new FormAttachment(100);
		}
		composite.setLayoutData(data);
		composite.setLayout(new FormLayout());
		return composite;
	}

	private Control createButtonBar(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		FormData data = new FormData();
		{
			data.top = new FormAttachment(0);
			data.bottom = new FormAttachment(100);
			data.right = new FormAttachment(100);
		}
		GridLayout layout = new GridLayout(0, true);
		{
			layout.marginWidth = 15;
			layout.horizontalSpacing = 4;
		}
		composite.setLayout(layout);
		composite.setLayoutData(data);
		return composite;
	}

	protected Control createBodyContent(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		FormData data = new FormData();
		{
			data.top = new FormAttachment(0);
			data.bottom = new FormAttachment(foot, 0, SWT.TOP);
			data.left = new FormAttachment(0);
			data.right = new FormAttachment(100);
		}
		composite.setLayoutData(data);
		return composite;
	}

	protected void createButtons(Composite parent) {
		createButton(parent, SWT.OK, "确定", true);
		createButton(parent, SWT.CANCEL, "取消", true);
	}

	protected Button createButton(Composite parent, int id, String label, boolean isDefault) {
		((GridLayout) parent.getLayout()).numColumns++;
		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setData(new Integer(id));
		GridData data = new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1);
		data.widthHint = 80;
		button.setLayoutData(data);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				buttonPressed(((Integer) event.widget.getData()).intValue());
			}
		});
		if (isDefault) {
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
		return button;
	}

	// ----------------------------------------------- 

	private void buttonPressed(int id) {
		switch (id) {
		case SWT.OK:
			okPressed();
			break;
		case SWT.CANCEL:
			cancelPressed();
			break;
		}
	}

	protected void okPressed() {
		setReturnCode(SWT.OK);
	}

	protected void cancelPressed() {
		setReturnCode(SWT.CANCEL);
	}

	public void setReturnCode(int returnCode) {
		this.returnCode = returnCode;
		close();
	}

	protected Point getInitialSize() {
		return new Point(600, 400);
	}

	// ----------------------------------------------- 

	public int open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return returnCode;
	}

	public void close() {
		shell.close();
	}

	// ----------------------------------------------- 
	
	public Shell getShell() {
		return shell;
	}

}
